<?php
namespace app\Logic\Layui\ServiceProviders;

use app\Logic\Container;
use app\Logic\Factory;
use app\Logic\Layui\Service\Register;
use App\Model\AdminUserModel;
use App\Model\AuthAccountModel;
use Illuminate\Support\Facades\DB;

class AdminUser implements ServiceProviderInterface
{

    public $app;


    public function __construct(\Closure $app)
    {
        $this->app = $app($this);
    }


    /**
     * @param Container $app
     */
    public static function register(Container $app)
    {
        !isset($app->values['AdminUser']) && $app->values['AdminUser'] = function (\Closure $app){
            return new self($app);
        };
    }



    public function lists()
    {
        $data = $this->app->input();

        $model = new AdminUserModel();

        $start = start($data['page'], $data['limit']);
        $res = $model->offset($start)->limit($data['limit'])
            ->orderBy('id', 'desc')->get();
        $count = $model->count();

        return ['code' => 0, 'count' => $count, 'data' => $res, 'msg' => ''];
    }


    public function create()
    {
        $data = $this->app->input();
        if(empty($data)){
            throw new \Exception('提交数据为空');
        }
        $model = new AdminUserModel();

        $rules = $model->getRules($data);
        $error = $model->validator($data, $rules);

        if($error){
            throw new \Exception($error);
        }

        $row = $model->where(['username'=>$data['username']])->first();
        if($row){
            throw new \Exception('该管理员已经存在');
        }

        $app = new self(function ($app){
            return new Register($app);
        });

        unset($data['password_confirmation']);
        $data['salt'] = $app->salt();
        $data['password'] = $app->encryptPassword($data['password'] , $data['salt']);
        $data['reg_ip'] = $this->app->getClientIp();
        $data['reg_time'] = date('Y-m-d H:i:s');
        $data['updated_at'] = date('Y-m-d H:i:s');

        $model->insert($data);

    }


    public function edit($id)
    {
        $data = $this->app->input();

        if(empty($data)){
            throw new \Exception('提交数据为空');
        }

        $model = new AdminUserModel();
        $rules = $model->getRules($data);
        $error = $model->validator($data, $rules);

        if($error){
            throw new \Exception($error);
        }
        $data['updated_at'] = date('Y-m-d H:i:s');

        $model->where(['id'=>$id])->update($data);
    }


    public function first($id)
    {
        $model = new AdminUserModel();

        return $model->where(['id'=>$id])->first()->toArray();
    }


    public function delete()
    {
        $ids = $this->app->input('id');

        return AdminUserModel::destroy($ids);
    }

    public function update($condition, $data)
    {
        $menuModel = new AdminUserModel();

        $menuModel->where($condition)->update($data);
    }

    public function resetPsd()
    {
        if($this->app->ajax()){

            $ids =  $this->app->input('id');
            $password = 123456;
            $app = new self(function ($app){
                return new Register($app);
            });

            $model = new AdminUserModel();
            DB::beginTransaction();
            try {

                foreach ($ids as $id){
                    $user = $model->where(['id'=>$id])->first();
                    $user->password = $app->encryptPassword($password , $user->salt);
                    $user->save();
                }

                DB::commit();

            } catch(\Illuminate\Database\QueryException $ex) {

                DB::rollback();
                throw new \Illuminate\Database\QueryException($ex->getMessage());

            } catch(\Exception $ex) {

                DB::rollback();
                throw new \Exception($ex->getMessage());
            }

        }else{

            throw new \Exception('请求类型错误');
        }
    }


    public function groupAuth($uid)
    {
        $data = $this->app->input();
        if(empty($data)){
            throw new \Exception('提交数据为空');
        }

        $data['extra_auth_id'] = isset($data['extra_auth_id']) ? implode(',',$data['extra_auth_id']) : '';
        $model = new AuthAccountModel();

        $row = $model->where(['uid'=>$uid])->first();
        if($row){
            $row->group_id = $data['group_id'];
            $row->extra_auth_id = $data['extra_auth_id'];
            $row->remark = $data['remark'];
            $row->updated_at = date('Y-m-d H:i:s');
            $row->save();
        }else{
            $data['uid'] = $uid;
            $data['created_at'] = date('Y-m-d H:i:s');
            $data['updated_at'] = date('Y-m-d H:i:s');
            $model->insert($data);
        }

    }


    /**
     * @param $name
     * @param $arguments
     * @return mixed
     */
    public function __call($name,$arguments)
    {
        return $this->app->$name(...$arguments);
    }
}